<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Welcome extends CI_Controller
{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		redirect('http://' . $_SERVER['HTTP_HOST'] . '/wap/home');
		//this->load->view('welcome_message');
	}

	public function yejitongji()
	{
		ignore_user_abort(true);
		ini_set('max_execution_time', '0');
		ini_set('memory_limit', '-1');
		if (empty($_GET['start_time'])) {
			$_GET['start_time'] = time();
		}
		if (empty($_GET['end_time'])) {
			$_GET['end_time'] = time() + 60 * 60;
		}
		if (!is_numeric($_GET['end_time'])) {
			$_GET['end_time'] = strtotime($_GET['end_time']);
		}
		if (!is_numeric($_GET['start_time'])) {
			$_GET['start_time'] = strtotime($_GET['start_time']);
		}
		$this->load->model('user_yeji_log_model');
		$this->load->model('user_yeji_model');

		while ($_GET['start_time'] < $_GET['end_time']) {
			$start_time = strtotime(date('Y-m-d', $_GET['start_time']) . '00:00:00');

			//计算出用户增长业绩
			$day = date('Y-m-d', $start_time);
			$where = array('day' => $day);

			$users = $this->user_yeji_log_model->getUserYejiList($where);

			if (!empty($users)) {

				//查找当前一天内已经更新的记录
				$userYejiList = $this->user_yeji_model->getListAll($where);
				if (!empty($userYejiList)) {
					$newUserYejiList = array();
					foreach ($userYejiList as $key => $value) {
						$newUserYejiList[$value['user_id']] = $value;
					}
					unset($userYejiList);
				}
				foreach ($users as $key => $item) {

					$yejiData = array(
						'user_id' => $item['user_id'],
						'zhitui' => $item['zhitui'] > 0 ? $item['zhitui'] : 0.000,
						'tuandui' => $item['tuandui'] > 0 ? $item['tuandui'] : 0.000,
						'tuandui_num' => $item['tuandui_num'] > 0 ? $item['tuandui_num'] : 0,
						'zhitui_num' => $item['zhitui_num'] > 0 ? $item['zhitui_num'] : 0,
						'update_time' => time(),
						'day' => $day,
					);

					if (isset($newUserYejiList[$item['user_id']])) {
						//更新
						$this->user_yeji_model->update(array('id' => $newUserYejiList[$item['user_id']]['id']), $yejiData);
					} else {
						$yejiData['create_time'] = time();
						$this->user_yeji_model->save($yejiData);
					}
				}
			}
			$_GET['start_time'] = strtotime(date('Y-m-d',strtotime('+1 day',$start_time)));
		}
		echo "更新成功";
	}
}
